import pymysql
my_cursor=None
conn=None
try:
# 创建连接
    conn = pymysql.Connect(host="172.28.166.245",port=3306,user = "root", password= "10086",database="student",charset="utf8")
#创建游标
    my_cursor=conn.cursor()
#执行update sql
    my_cursor.execute("update course set studentId = '2022212999' WHERE courseId = 1;")
    print("Affected rows:", conn.affected_rows())
#提交事务
    conn.commit()
except Exception as err:
    print("更新sql失败：", str(err))
    conn.rollback()

finally:
   my_cursor.close()
   my_cursor.close()